<?php
/**
 * Created by PhpStorm.
 * User: jucai002
 * Date: 2019/1/21
 * Time: 16:40
 */

namespace backend\controllers;


use app\models\Admin;
use frontend\controllers\BaseController;

class LoginController extends BaseController
{

    public function actionLogin()
    {
        if (empty(\Yii::$app->request->get('username'))) {
            return $this->render('login');
        } else {
            //验证登录
            $userdata = \Yii::$app->request->get('username', 1);
            $pwd = \Yii::$app->request->get('pwd', 1);
            //查库
            $admin = Admin::find()->where(['userdata' => $userdata, 'pwd' => $pwd])->one();

            if (empty($admin)) {
                echo json_encode(['code'=>400,'message'=>'密码错误']);exit;
            } else {
                //存储session
                $_SESSION['log']=json_encode([
                    'username'=>$userdata,
                ]);
                echo json_encode(['code'=>200,'message'=>$_SESSION['log']]);exit;
            }
        }
    }

    public function actionSign_out()
    {
        unset($_SESSION['log']);
        $this->redirect(\Yii::$app->urlManager->createUrl(['login/login']));
    }


}